
//--------------------------------------------------------------------
//     此代码由T4模板自动生成
//	   生成时间 2021-09-07 19:42:15 
//     对此文件的更改可能会导致不正确的行为，并且如果重新生成代码，这些更改将会丢失。
//--------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using TianYu.CMS.Domain.DomainModel;
using TianYu.CMS.Domain.ViewModel.Request; 
using TianYu.CMS.Service;
using TianYu.Core.Common;
using TianYu.Core.Common.BaseViewModel;
using SqlSugar;
using TianYu.CMS.Domain.ViewModel.Response;

namespace TianYu.CMS.WebMvc.Areas.Admin.Controllers
{
    public class AdvController : BaseController
    {
        private IAdvService _advService;
        private IAdvPositionService _advPositionService;
        public AdvController(IAdvService advService, IAdvPositionService advPositionService)
        {
            this._advService = advService;
            this._advPositionService = advPositionService;
        }



        public IActionResult Index()
        {
            return View();
        }
        public async Task<IActionResult> AddEdit(string guid)
        {
            var model = new Adv();
            ViewBag.AdvPositionList = await _advPositionService.QueryAsync();
            if (!string.IsNullOrWhiteSpace(guid))
            {
                model = await _advService.FindByIdAsync(guid);
            }

            ViewBag.BeginEndTime = model.StartTime.HasValue ? (model.StartTime.Value.ToString("yyyy-MM-dd HH:mm:ss") + " ~ " + model.EndTime.Value.ToString("yyyy-MM-dd HH:mm:ss")) : "";

            return View(model);
        }




        public async Task<AjaxPageResult<List<AdvQueryResponseModel>>> GetList([FromQuery] AdvListRequestModel requestModel)
        {
            var filter = Expressionable.Create<Adv>()
                .And(o => o.IsDel == false)
                .AndIF(!requestModel.KeyWords.IsNullOrWhiteSpace(), o => o.AdvName.Contains(requestModel.KeyWords))
                .ToExpression();

            var res = await _advService.QueryPageAsync(filter, o => o.CreateTime, OrderByType.Desc, requestModel.PageIndex, requestModel.PageSize);
            var resList = new List<AdvQueryResponseModel>();
            if (res.list.Any())
            {
                resList = res.list.MapToList<AdvQueryResponseModel>();
                var ids = res.list.Select(x => x.AdvPositionId);

                var posList = await _advPositionService.QueryAsync(x => ids.Contains(x.Id));

                if (posList.Any())
                {
                    resList.ForEach(m =>
                    {
                        var entity = posList.Where(x => x.Id == m.AdvPositionId).FirstOrDefault();
                        if (entity != null)
                            m.PosName = entity.Name;
                    });
                }
            }

            return resList.ToViewModel(requestModel.PageIndex, res.total);
        }
        public async Task<AjaxResult<bool>> Save(Adv requestModel)
        {
            if (requestModel.AdvName.IsNullOrWhiteSpace())
            {
                return AjaxResult<bool>.Fail("广告名称不能为空");
            }

            var isExits = await _advService.AnyAsync(x => x.Id != requestModel.Id && x.AdvName == requestModel.AdvName);
            if (isExits)
            {
                return AjaxResult<bool>.Fail($"广告名称[{requestModel.AdvName}]已存在，不能重复添加");
            }

            if (requestModel.Id > 0)
            {
                var res = await _advService.UpdateAsync(o => new Adv()
                {
                    AdvName = requestModel.AdvName,
                    AdvDesc = requestModel.AdvDesc,
                    AdvImg = requestModel.AdvImg,
                    AdvPositionId = requestModel.AdvPositionId,
                    JmpUrl = requestModel.JmpUrl,
                    StartTime = requestModel.StartTime,
                    EndTime = requestModel.EndTime
                }, x => x.Id == requestModel.Id);

                return res ? AjaxResult<bool>.Success() : AjaxResult<bool>.Fail("保存失败");
            }
            else
            {
                requestModel.CreateTime = DateTime.Now;

                await _advService.InsertAsync(requestModel);

                return AjaxResult<bool>.Success();
            }
        }
        public async Task<AjaxResult<bool>> Remove(long[] ids)
        {
            bool result = await _advService.UpdateAsync(o => new Adv()
            {
                IsDel = true
            }, o => ids.Contains(o.Id));

            return result ? AjaxResult<bool>.Success() : AjaxResult<bool>.Fail("删除失败");
        }
    }
}

